﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Cleverscape.UTorrentClient.WebClient;

namespace uTorrentNetClient.ModelDesign.Converter
{
    [ValueConversion(typeof(TorrentSimpleStatus), typeof(System.Windows.Media.ImageSource))]
    public class StatusImageDisplayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(System.Windows.Media.ImageSource))
            {
                return null;
            }
            if (value is TorrentSimpleStatus)
            {
                TorrentSimpleStatus StatusToConvert = (TorrentSimpleStatus)value;
                switch (StatusToConvert)
                {
                    case TorrentSimpleStatus.DownloadingQueued:
                        return BitmapToImageSource(Properties.Resources.Status7DownloadQueued);
                    case TorrentSimpleStatus.Downloading:
                        return BitmapToImageSource(Properties.Resources.Status1Downloading);
                    case TorrentSimpleStatus.DownloadingError:
                        return BitmapToImageSource(Properties.Resources.Status5Error);
                    case TorrentSimpleStatus.InactiveIncomplete:
                        return BitmapToImageSource(Properties.Resources.Status3InactiveIncomplete);
                    case TorrentSimpleStatus.SeedingQueued:
                        return BitmapToImageSource(Properties.Resources.Status8SeedingQueued);
                    case TorrentSimpleStatus.Seeding:
                        return BitmapToImageSource(Properties.Resources.Status2Seeding);
                    case TorrentSimpleStatus.SeedingError:
                        return BitmapToImageSource(Properties.Resources.Status5Error);
                    case TorrentSimpleStatus.InactiveCompleted:
                        return BitmapToImageSource(Properties.Resources.Status6Finished);
                    case TorrentSimpleStatus.Paused:
                        return BitmapToImageSource(Properties.Resources.Status4Paused);
                    case TorrentSimpleStatus.Error:
                        return BitmapToImageSource(Properties.Resources.Status5Error);
                    default:
                        break;
                }
            }
            return null;
        }

        private System.Windows.Media.ImageSource BitmapToImageSource(System.Drawing.Bitmap InputBitmap)
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    InputBitmap.GetHbitmap(),
                    IntPtr.Zero,
                    System.Windows.Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
